//
//  ImageViewController.m
//  Focus
//
//  Created by 楼主 on 14-11-9.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ImageViewController.h"
#import "imageCell.h"
#import "NetHandler.h"
#import "UIImageView+WebCache.h"
@interface ImageViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *myCollectionView;
@property (nonatomic, retain)NSMutableDictionary *dicData;
@property (nonatomic, retain)NSMutableArray *articlesArr;
@property (nonatomic, retain)NSMutableArray *pagesArr;
@property (nonatomic, assign)NSInteger height;
@property (nonatomic, retain)NSMutableArray *dataArray;
@property (nonatomic, retain)NSMutableArray *mediaArray;
@property (nonatomic, retain)NSMutableArray *imageArray;
@property (nonatomic, retain)NSString *blockImageStr;
@property (nonatomic, retain)NSMutableArray *bigArray;
@property (nonatomic, retain)NSString *titleImageStr;
@end

@implementation ImageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dicData = [NSMutableDictionary dictionary];
    self.articlesArr = [NSMutableArray array];
    self.pagesArr = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    self.mediaArray = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    self.bigArray = [NSMutableArray array];
    
    [NetHandler networkWithURL:self.api_url comaletion:^(id result) {
        
        NSLog(@"%@",self.api_url);
        self.dicData = result;
        self.articlesArr = [[self.dicData objectForKey:@"data"] objectForKey:@"articles"];
        self.pagesArr = [[[self.dicData objectForKey:@"data"] objectForKey:@"ipadconfig"] objectForKey:@"pages"];
        
        for (NSDictionary *dic in self.articlesArr) {
            
            int count = [[dic objectForKey:@"media"] count];
            
            NSString *str = [NSString string];
            for (int i = 0; i < count; i++) {
                str = [[dic objectForKey:@"media"][i] objectForKey:@"url"];
                [self.bigArray addObject:str];
                //                [self.bigArray addObject:self.imageArray];
            }
        }
//        NSLog(@"%@",self.bigArray);
        
        NSDictionary *blockDic = [self.dicData objectForKey:@"block_info"];
        self.blockImageStr = [blockDic objectForKey:@"pic"];
        
        self.titleImageStr = [[[self.dicData objectForKey:@"data"] objectForKey:@"block_info"] objectForKey:@"pic"];
        [self setCollection];
    }];
}
#pragma mark 创建collection
- (void)setCollection
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //cell之间的间距
    flowLayout.minimumInteritemSpacing = 0;
    //cell行之间的间距
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 0, 1, 1);
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.pagingEnabled = YES;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"mei"];
    [self.view addSubview:self.myCollectionView];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 530, 320, 40)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
#pragma mark 创建取消button
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(0, self.view.frame.size.height - 45, 50, 50);
    [cancelButton addTarget:self action:@selector(button_backClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:cancelButton];
    
    
}
#pragma mark 推出button
- (void)button_backClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.bigArray.count / 7;
}
#pragma mark cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mei" forIndexPath:indexPath];
    
    //    for (NSString *rectStr in arr) {
    //        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectFromString(rectStr)];
    //
    //        [self.view addSubview:imageview];
    //
    //    }
    [cell.imageOne sd_setImageWithURL:[NSURL URLWithString:self.bigArray[0 + indexPath.row * 6]]];
    [cell.imageTwo sd_setImageWithURL:[NSURL URLWithString:self.bigArray[1 + indexPath.row * 6]]];
    [cell.imageThr sd_setImageWithURL:[NSURL URLWithString:self.bigArray[2 + indexPath.row * 6]]];
    [cell.imageFou sd_setImageWithURL:[NSURL URLWithString:self.bigArray[3 + indexPath.row * 6]]];
    [cell.imageFive sd_setImageWithURL:[NSURL URLWithString:self.titleImageStr]];
    [cell.imageSix sd_setImageWithURL:[NSURL URLWithString:self.bigArray[4 + indexPath.row * 6]]];
    [cell.imageSeven sd_setImageWithURL:[NSURL URLWithString:self.bigArray[5 + indexPath.row * 6]]];
    [cell.imageEight sd_setImageWithURL:[NSURL URLWithString:self.bigArray[6 + indexPath.row * 6]]];

    return cell;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
